using System.Web.Mvc;
using GoldenBicycleCompany.Core;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Core.DomainModel;
using System.Collections.Generic;
using System;
using SharpArch.Web.NHibernate;
using NHibernate.Validator.Engine;
using System.Text;
using SharpArch.Web.CommonValidator;
using SharpArch.Core;

namespace GoldenBicycleCompany.Web.Controllers
{
    [HandleError]
    public class CustomerAddressesController : Controller
    {
        public CustomerAddressesController(IRepository<CustomerAddress> customerAddressRepository) {
            Check.Require(customerAddressRepository != null, "customerAddressRepository may not be null");

            this.customerAddressRepository = customerAddressRepository;
        }

        [Transaction]
        public ActionResult Index() {
            IList<CustomerAddress> customerAddresses = customerAddressRepository.GetAll();
            return View(customerAddresses);
        }

        [Transaction]
        public ActionResult Show(int id) {
            CustomerAddress customerAddress = customerAddressRepository.Get(id);
            return View(customerAddress);
        }

        public ActionResult Create() {
            CustomerAddressFormViewModel viewModel = CustomerAddressFormViewModel.CreateCustomerAddressFormViewModel();
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(CustomerAddress customerAddress) {
            if (ViewData.ModelState.IsValid && customerAddress.IsValid()) {
                customerAddressRepository.SaveOrUpdate(customerAddress);

                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The customerAddress was successfully created.";
                return RedirectToAction("Index");
            }

            CustomerAddressFormViewModel viewModel = CustomerAddressFormViewModel.CreateCustomerAddressFormViewModel();
            viewModel.CustomerAddress = customerAddress;
            return View(viewModel);
        }

        [Transaction]
        public ActionResult Edit(int id) {
            CustomerAddressFormViewModel viewModel = CustomerAddressFormViewModel.CreateCustomerAddressFormViewModel();
            viewModel.CustomerAddress = customerAddressRepository.Get(id);
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(CustomerAddress customerAddress) {
            CustomerAddress customerAddressToUpdate = customerAddressRepository.Get(customerAddress.Id);
            TransferFormValuesTo(customerAddressToUpdate, customerAddress);

            if (ViewData.ModelState.IsValid && customerAddress.IsValid()) {
                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The customerAddress was successfully updated.";
                return RedirectToAction("Index");
            }
            else {
                customerAddressRepository.DbContext.RollbackTransaction();

				CustomerAddressFormViewModel viewModel = CustomerAddressFormViewModel.CreateCustomerAddressFormViewModel();
				viewModel.CustomerAddress = customerAddress;
				return View(viewModel);
            }
        }

        private void TransferFormValuesTo(CustomerAddress customerAddressToUpdate, CustomerAddress customerAddressFromForm) {
			customerAddressToUpdate.Customer = customerAddressFromForm.Customer;
			customerAddressToUpdate.Address1 = customerAddressFromForm.Address1;
			customerAddressToUpdate.Address2 = customerAddressFromForm.Address2;
			customerAddressToUpdate.City = customerAddressFromForm.City;
			customerAddressToUpdate.State = customerAddressFromForm.State;
			customerAddressToUpdate.ZipCode = customerAddressFromForm.ZipCode;
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            string resultMessage = "The customerAddress was successfully deleted.";
            CustomerAddress customerAddressToDelete = customerAddressRepository.Get(id);

            if (customerAddressToDelete != null) {
                customerAddressRepository.Delete(customerAddressToDelete);

                try {
                    customerAddressRepository.DbContext.CommitChanges();
                }
                catch {
                    resultMessage = "A problem was encountered preventing the customerAddress from being deleted. " +
						"Another item likely depends on this customerAddress.";
                    customerAddressRepository.DbContext.RollbackTransaction();
                }
            }
            else {
                resultMessage = "The customerAddress could not be found for deletion. It may already have been deleted.";
            }

            TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = resultMessage;
            return RedirectToAction("Index");
        }

		/// <summary>
		/// Holds data to be passed to the CustomerAddress form for creates and edits
		/// </summary>
        public class CustomerAddressFormViewModel
        {
            private CustomerAddressFormViewModel() { }

			/// <summary>
			/// Creation method for creating the view model. Services may be passed to the creation 
			/// method to instantiate items such as lists for drop down boxes.
			/// </summary>
            public static CustomerAddressFormViewModel CreateCustomerAddressFormViewModel() {
                CustomerAddressFormViewModel viewModel = new CustomerAddressFormViewModel();
                
                return viewModel;
            }

            public CustomerAddress CustomerAddress { get; internal set; }
        }

        private readonly IRepository<CustomerAddress> customerAddressRepository;
    }
}
